#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int t; 
    cin >> t;
    while(t--){
        string s;
        cin >> s;
        vector<int> a(10);
        for(int i = 0; i < s.length(); ++i){
            a[s[i]-'0']++;
        }
        sort(a.begin(),a.end(), greater<int>());
        if(a[0] <= 2) cout << 4 << '\n';
        else if(a[0] == 3) cout << 6 << '\n';
        else cout << -1 << '\n';
    }
    
    return 0;
}